import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import moment from 'moment';

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: import("@/pages/login.vue"),
  },
  // TODO: 填写其他页面路由内容
  {
    path: "/home",
    name: "Home",
    redirect: "/homePage",
    component: import("@/components/home/home.vue"),
    children: [
      {
        path: "/homePage",
        name: "HomePage",
        component: import("@/components/send/homePage/homePage.vue"),
      },

      //食品router<同一层级，未作children>
      {
        path: "/foot",
        name: "footList",
        component: import("@/components/send/footList/footList.vue"),
      },
      {
        path: "/addfoot",
        name: "addFoot",
        component: import("@/components/send/footList/footOperation/addFoot.vue"),
      },
      {
        path: "/modifyfoot",
        name: "modifyFoot",
        component: import("@/components/send/footList/footOperation/modifyFoot.vue"),
      },
      {
        path: "/delfoot",
        name: "delFoot",
        component: import("@/components/send/footList/footOperation/delFoot.vue"),
      },

      //景点 region
      {
        path: "/scenery",
        name: "scenery",
        component: import("@/components/send/scenery/scenery.vue"),
      },
      {
        path: "/addscenery",
        name: "addScenery",
        component: import("@/components/send/scenery/sceneryModole/addScenery.vue"),
      },
      {
        path: "/modifyscenery",
        name: "modifyScenery",
        component: import("@/components/send/scenery/sceneryModole/modifyScenery.vue"),
      },
      {
        path: "/delscenery",
        name: "delScenery",
        component: import("@/components/send/scenery/sceneryModole/delScenery.vue"),
      },

      //节日 region
      {
        path: "/festival",
        name: "festival",
        component: import("@/components/send/festival/festival.vue"),
      },
      {
        path: "/addfestival",
        name: "addFestival",
        component: import("@/components/send/festival/festivalModule/addFestival.vue"),
      },
      {
        path: "/modifyfestival",
        name: "modifyFestival",
        component: import("@/components/send/festival/festivalModule/modifyFestival.vue"),
      },
      {
        path: "/delfestival",
        name: "delFestival",
        component: import("@/components/send/festival/festivalModule/delFestival.vue"),
      },

      //简介 region
      {
        path: "/introduction",
        name: "Introduction",
        component: import("@/components/send/introduction/introduction.vue"),
      },
      {
        path: "/modyfiIntroduction",
        name: "modyfiIntroduction",
        component: import("@/components/send/introduction/modifyintroduction.vue"),
      },
    ],
  },
  //静态导航
  {
    path: "/",
    component: import("@/pages/login.vue"),
  },
  //404
  {
    // 匹配全部其他内容
    path: "/:pathMatch(.*)*",
    component: () => import(/* webpackChunkName: "404" */ "@/pages/404.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由拦截器
router.beforeEach((to, from, next) => {
  // TODO: 自定义拦截内容
  if (to.path !== '/login') {
    // 对token进行验证 （验证token的接口）
    if(localStorage.getItem('token')){
      const dataStart:any = localStorage.getItem('tokenStartTime')
      const data:any = moment(parseInt(dataStart)).add('1','days')
      const newData:any = moment(new Date())
      if(data-newData<=0){
        next('/login')
      }else{
        next()
      }
    }
    // .catch不需要再写了，因为在axios的响应拦截器中已经进行了401的判断
    /* .catch(() => {
      next('/login')
    })  */

  } else {
    next()
  }
});

export default router;
